﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Drawing;
using System.Web.SessionState;
using System.Windows.Forms;
using Protocolo;
using Protocolo.Exception;

public partial class Calendario : Page
{
    protected string[] DiasMarcados = new string[5];
    protected IList eventos;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string mes = DateTime.Now.ToString("MM/yyyy");
            eventos = Control_calendario.busca_evento(mes);
            Calendar1.SelectedDate = DateTime.Now;
        }
        catch( ConsultaException ce )
        {
            MessageBox.Show("Ha ocurrido un error, por favor intente mas tarde.");
            Response.Redirect("Default.aspx");
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        Label1.Visible = true;
        Calendar1.SelectedDate = DateTime.Now;
    }
    
    public void ParseCalendar(object sender, DayRenderEventArgs  e)
    {
        if (eventos != null)
        {
            for (int i = 0; i < eventos.Count; i++)
            {
                string[] arrayFecha = e.Day.Date.ToString().Split(new char[] {' '});
                string[] fecha = ((DateTime) eventos[i]).ToString().Split(new char[] {' '});
                if (arrayFecha[0] == fecha[0])
                {
                    if (fecha[0].Equals(DateTime.Now.ToString("dd/MM/yyyy")))
                    {
                        e.Cell.BackColor = Color.MidnightBlue;
                        e.Cell.ForeColor = Color.Red;
                    }
                    else
                    {
                        e.Cell.BackColor = Color.SteelBlue;
                        e.Cell.ForeColor = Color.Red;
                    }
                    e.Cell.ToolTip = DiasMarcados[i];
                    string[] fecha2 = fecha[0].Split(new char[] {'/'});
                    e.Cell.Text = "<a href='procesar_eventos.aspx?dia=" + fecha2[0] + "&mes=" + fecha2[1] + "&an=" +
                                  fecha2[2] + "'>" + e.Day.DayNumberText + "</a>";
                }
            }
        }
    }

    protected void Monthchanged(object sender, MonthChangedEventArgs e)
    {
        Label1.Visible = false;
        try
        {
            string mes = e.NewDate.Date.ToString("MM/yyyy");
            eventos = Control_calendario.busca_evento(mes);
        }
        catch ( ConsultaException ce )
        {
            MessageBox.Show("Ha ocurrido un error, por favor intentelo mas tarde.");
            Response.Redirect("Default.aspx");
        }
        //throw new NotImplementedException();
    }
}
